package cn.paper;

import cn.print.Paper;

public class TextPaper implements Paper {
    private int posX = 0;  // 打印位置的横坐标
    private int posY = 0;  // 打印位置的纵坐标
    private int posP = 1;  // 当前页码
    private int charPerLine = 16;  // 每行能打印多少字符
    private int linePerPage = 5;  // 每页打印多少行
    private String content ; // 纸张中的内容

    @Override
    public void putInChar(char c) {
        content += c ;
        ++posX;
        // 换行操作
        if ( posX == charPerLine ) {
            content += Paper.newline;
            posX = 0;
            ++ posY ;
        }
        // 换页操作
        if( posY == linePerPage ) {
            content += "第" + posP + "页" ;
            content += Paper.newline + Paper.newline;
            posY = 0 ;
            ++ posP ;
        }

    }

    @Override
    public String getContent() {
        String ret = this.content;
        if ( !(posX == 0 && posY == 0 )  ) {
            int count = linePerPage - posY ;
            for ( int i =0 ; i < count ; ++ i ) {
                ret += Paper.newline ;
            }
            ret += " 第 " + posP + "页" ;
        }
        return ret;
    }

    public void setCharPerLine(int charPerLine) {
        this.charPerLine = charPerLine;
    }

    public void setLinePerPage(int linePerPage) {
        this.linePerPage = linePerPage;
    }
}



